package pCloud.acesscontrol;

import java.io.FileOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import com.sun.xacml.Indenter;
import com.sun.xacml.Policy;
import com.sun.xacml.Rule;
import com.sun.xacml.Target;
import com.sun.xacml.combine.CombiningAlgFactory;
import com.sun.xacml.combine.OrderedPermitOverridesRuleAlg;
import com.sun.xacml.combine.RuleCombiningAlgorithm;
import com.sun.xacml.ctx.Result;

/**
 * @author ttadinh
 * Translate a data owner policy into XACML policy
 * The former contains: stream ID, a key subject match and string match
 */
public class PolicyHelper {
	
	//create simple policy: resource = streamId, subject key=val
	public static void createSimplePolicy(int streamId, int policyId, int keyVal, String fileName) throws Exception{
		URI id = new URI(policyId+"");
		URI combiningAlgId = new URI(OrderedPermitOverridesRuleAlg.algId);
        CombiningAlgFactory factory = CombiningAlgFactory.getInstance();
        RuleCombiningAlgorithm combiningAlg =
            (RuleCombiningAlgorithm)(factory.createAlgorithm(combiningAlgId));
        
        ResourceMatchInteger rmi = new ResourceMatchInteger(streamId);
        SubjectMatchIntegerEqual smie = new SubjectMatchIntegerEqual(keyVal);
        Target target = new Target(smie.getMatchTarget(), rmi.getMatchTarget(), null);
        
        Rule defaultRule = new Rule(new URI("Default"), Result.DECISION_PERMIT,
                null, null, null);
        List ruleList = new ArrayList();
        ruleList.add(defaultRule);
        
        Policy policy = new Policy(id, combiningAlg, null, target, null, ruleList, null);
        policy.encode(new FileOutputStream(fileName), new Indenter());
        
	}
	
	//create simple policy: resource = streamId, subject key=val
	public static void createSlidingWindowPolicy(int streamId, int policyId, int keyVal, String fileName) throws Exception{
			URI id = new URI(policyId+"");
			URI combiningAlgId = new URI(OrderedPermitOverridesRuleAlg.algId);
	        CombiningAlgFactory factory = CombiningAlgFactory.getInstance();
	        RuleCombiningAlgorithm combiningAlg =
	            (RuleCombiningAlgorithm)(factory.createAlgorithm(combiningAlgId));
	        
	        ResourceMatchInteger rmi = new ResourceMatchInteger(streamId);
	        SubjectMatchIntegerLE smige = new SubjectMatchIntegerLE(keyVal);
	        Target target = new Target(smige.getMatchTarget(), rmi.getMatchTarget(), null);
	        
	        Rule defaultRule = new Rule(new URI("Default"), Result.DECISION_PERMIT,
	                null, null, null);
	        List ruleList = new ArrayList();
	        ruleList.add(defaultRule);
	        
	        Policy policy = new Policy(id, combiningAlg, null, target, null, ruleList, null);
	        policy.encode(new FileOutputStream(fileName), new Indenter());
		}
		
	public static void main(String[] args) throws Exception{
		PolicyHelper.createSlidingWindowPolicy(0, 0, 5, "policy_0_0");
		PolicyHelper.createSlidingWindowPolicy(0,1,2, "policy_0_1");
	}
}
